﻿namespace QuickMail.ViewModels
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Media.Imaging;
    using DbWorker.DataModels;
    using QuickMail.DataModels;
    using QuickMail.BusinessLogic;
    
    public class RuleVm : INotifyPropertyChanged
    {
        #region Properties 

        /// <summary>
        /// Title of rule.
        /// </summary>
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                OnPropertyChanged("Title");
            }
        }

        /// <summary>
        /// Action (none, sending, receiving)
        /// </summary>
        public RuleAction Action
        {
            get
            {
                return _action;
            }
            set
            {
                _action = value;
                SetActionIcon(_action);
            }
        }

        /// <summary>
        /// Image of rule.
        /// </summary>
        public BitmapImage ActionImage
        {
            get
            {
                return _actionImage;
            }
            set
            {
                _actionImage = value;
                OnPropertyChanged("ActionImage");
            }
        }

        /// <summary>
        /// Children rules
        /// </summary>
        public ObservableCollection<RuleVm> Childrens { get; set; }

        #endregion      //End Properties

        #region Fields

        /// <summary>
        /// Id of folder rule
        /// </summary>
        public long Id;

        public long ParentId;

        /// <summary>
        /// Path to directory
        /// </summary>
        public string DirectoryPath;

        /// <summary>
        /// The addresses of senders/recipients
        /// </summary>
        public string Addresses;

        /// <summary>
        /// Subject letter
        /// </summary>
        public string Subject;

        /// <summary>
        /// The addresses of senders/recipients
        /// </summary>
        public List<string> AddressList;

        /// <summary>
        /// File types which will participate in process 
        /// </summary>
        public string FilesTypes;

        /// <summary>
        /// File types which will participate in process 
        /// </summary>
        public List<string> FilesTypeList;

        /// <summary>
        /// Action with the same file names
        /// </summary>
        public ReplaceFile Replace;

        /// <summary>
        /// The message text
        /// </summary>
        public string MessageText;

        /// <summary>
        /// Save or not to save the text of the letter.
        /// </summary>
        public bool SaveMessage;

        /// <summary>
        /// Get text for letter from file.
        /// </summary>
        public bool MessageFromFile;

        private string _title;
        private RuleAction _action;
        private BitmapImage _actionImage = StaticFields.NoneImage;

        #endregion  // End Fields

        /// <summary>
        /// Event of change property.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;


        public RuleVm()
        {
            Childrens = new ObservableCollection<RuleVm>();
        }

        public RuleVm(RuleDm rule)
        {
            Id = rule.Id;
            ParentId = rule.ParentId;
            Title = rule.Title;
            DirectoryPath = rule.DirectoryPath;
            Action = rule.Action;
            Subject = rule.Subject;
            Addresses = rule.Addresses;
            AddressList = HelpWorker.DivideWords(rule.Addresses);
            FilesTypes = rule.FileTypes;
            FilesTypeList = HelpWorker.DivideWords(rule.FileTypes);
            Replace = rule.Replace;
            SaveMessage = rule.SaveMessage;
            MessageText = rule.MessageText;
            MessageFromFile = rule.MessageFromFile;

            Childrens = new ObservableCollection<RuleVm>();
        }
        public RuleDm CreateDataModel()
        {
            return new RuleDm
            {
                // ReSharper disable RedundantThisQualifier
                Id = this.Id,
                Title = this.Title,
                DirectoryPath = this.DirectoryPath,
                Action = this.Action,
                Subject = this.Subject,
                Addresses = this.Addresses,
                FileTypes = this.FilesTypes,
                Replace = this.Replace,
                SaveMessage = this.SaveMessage,
                MessageText = this.MessageText,
                MessageFromFile = this.MessageFromFile,
                ParentId = this.ParentId
                // ReSharper restore RedundantThisQualifier
            };
        }

        /// <summary>
        /// An event handler property changes.
        /// </summary>
        /// <param name="propertyName">Название свойства</param>
        private void OnPropertyChanged(string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Sets the icon for the rule.
        /// </summary>
        /// <param name="action"></param>
        private void SetActionIcon(RuleAction action)
        {
            switch (action)
            {
                case RuleAction.None:
                    ActionImage = StaticFields.NoneImage;
                    break;
                case RuleAction.Send:
                    ActionImage = StaticFields.SendImage;
                    break;
                case RuleAction.Receive:
                    ActionImage = StaticFields.ReceiveImage;
                    break;
            }
        }
    }
}
